package com.vcc.web.controller;

import com.vcc.core.annotations.CurrentClientIp;
import com.vcc.core.annotations.RestPack;
import com.vcc.core.util.JwtUtil;
import com.vcc.data.entity.User;
import com.vcc.web.core.config.AccountConfig;
import com.vcc.service.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("token")
public class TokenController {

    @Autowired
    private UserService userService;
    @Autowired
    private AccountConfig accountConfig;


    @RestPack
    @GetMapping("getRoToken")
    public Boolean getRoToken(@CurrentClientIp String clientIp, Long userId, HttpServletResponse httpServletResponse) {
        User user = userService.getById(userId);
        String token = JwtUtil.createRoJWT(user.getId(), accountConfig.getJwtExpiresSecond(), accountConfig.getJwtSecretString(), clientIp);
        httpServletResponse.setHeader("token", token);
        httpServletResponse.setHeader("Access-Control-Expose-Headers", "token");
        return true;
    }
}
